<?php
include("/usr/share/jpgraph/jpgraph.php");
include("/usr/share/jpgraph/jpgraph_bar.php");

require_once("config.php");
require_once("CMySQL.php");

$codeClient="";
if(isset($_GET["codeClient"]))
{
	$codeClient=$_GET["codeClient"];
}


if(isset($_GET["aPartirDe"]))
{
	define("PREM_MOIS", $_GET["aPartirDe"]);
}else 
{
	define("PREM_MOIS", 1);
}

define("NB_ANNEE", 3);
define("NB_MOIS", 12);

$months=array("Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre");
$labels=array();

for($i=1;$i<PREM_MOIS;$i++)
{
	$labels[]=array_shift($months);
}
$labels=array_merge($months, $labels);

date_default_timezone_set("Europe/Paris");
$today=getdate();

$lastYear=$today["year"];
if($today["mon"] >= PREM_MOIS)
{
    $lastYear++;
}
$firstYear=$lastYear-NB_ANNEE;

$lastMois=PREM_MOIS-1;
if($lastMois == 0)
{
	$lastMois=12;
	$firstYear++;
}

$periodeMin=$firstYear."-".str_pad(PREM_MOIS, 2, "0", STR_PAD_LEFT);
$periodeMax=$lastYear."-".str_pad($lastMois, 2, "0", STR_PAD_LEFT);

$graph=new Graph(800, 600, "auto");
$graph->title->Set("Chiffre d'affaire cumulé sur 12mois");
$graph->title->SetFont(FF_VERDANA,FS_NORMAL,12);
$graph->SetScale("textlin");
$graph->xaxis->SetTickLabels($labels);
$graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->xaxis->SetLabelAngle(50);
$graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->legend->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->legend->pos(0.005, 0.005);

$dbName="";
if(isset($_GET["dossier"])) {
	$dbName=CMySQL::getInstance()->getDbNameFromDossier($_GET["dossier"]);
}

$sql="SELECT DATE_FORMAT(creation, \"%Y-%m\") AS periode, SUM(prixUnitaireHT*quantite*IF(document=\"A\",-1,1)) AS ca ";
$sql.="FROM teteFactures INNER JOIN ligneFactures ON facture=code ";
$sql.="WHERE DATE_FORMAT(creation, \"%Y-%m\") BETWEEN \"{$periodeMin}\" AND \"{$periodeMax}\" AND codeClient=\"${codeClient}\" ";
$sql.="GROUP BY periode ORDER BY periode";

$sqlAFact="SELECT DATE_FORMAT(creation, \"%Y-%m\") AS periode, SUM(prixUnitaireHT*quantite*IF(document=\"A\",-1,1)) AS ca ";
$sqlAFact.="FROM ligneBLs INNER JOIN teteBLs ON bl = code ";
$sqlAFact.="WHERE blFacture(code)=0 AND DATE_FORMAT(creation, \"%Y-%m\") BETWEEN \"{$periodeMin}\" AND \"{$periodeMax}\" AND codeClient=\"${codeClient}\" ";
$sqlAFact.="GROUP BY periode";

CMySQL::getInstance()->selectDb($dbName);

$result=CMySQL::getInstance()->execQuery($sql);
$resultAFact=CMySQL::getInstance()->execQuery($sqlAFact);

$data=array();
$dataAFact=array();

$row=mysql_fetch_array($result);
$rowAFact=mysql_fetch_array($resultAFact);

$annee=$firstYear;
$mois=PREM_MOIS;

$bplots=array();
$bplotsAFact=array();
$accBarPlot=array();

$color=array("blue", "orange", "green");
$idAnnee=0;

$value=0;
$valueAFact=0;
$cpt=0;
do
{
	$periode=$annee."-".str_pad($mois, 2, "0", STR_PAD_LEFT);

	//echo $periode."<br />";

	if($row)
	{
		if($periode == $row["periode"])
		{
			$value+=$row["ca"]*1;
			$row=mysql_fetch_array($result);
		}	
	}
	
	if($rowAFact)
	{
		if($periode == $rowAFact["periode"])
		{
			$valueAFact+=$rowAFact["ca"]*1;
			$rowAFact=mysql_fetch_array($resultAFact);
		}	
	}
	$data[$idAnnee][$cpt]=$value;
	$dataAFact[$idAnnee][$cpt]=$valueAFact;

	$cpt++;
	$mois++;
	if($mois > NB_MOIS)
	{
		$mois=1;
		$annee++;
	}
	if($cpt == NB_MOIS)
	{
		$anneeN=$idAnnee+1-NB_ANNEE;
		$anneeN="Année n".($anneeN != 0 ? $anneeN : "");

		$bplots[$idAnnee]=new BarPlot($data[$idAnnee]);
		$bplots[$idAnnee]->SetFillColor($color[$idAnnee]);
		$bplots[$idAnnee]->SetLegend($anneeN);
		
		$bplotsAFact[$idAnnee]=new BarPlot($dataAFact[$idAnnee]);
		$bplotsAFact[$idAnnee]->SetFillColor("red");
		
		$accBarPlot[$idAnnee] = new AccBarPlot(array($bplots[$idAnnee], $bplotsAFact[$idAnnee]));
		
		$idAnnee++;
		$mois=PREM_MOIS;
		
		$value=0;
		$valueAFact=0;

		$cpt=0;
	}

}while($periode < $periodeMax);

$bplotsAFact[NB_ANNEE-1]->SetLegend("Reste à facturer");
$gbarplot=new GroupBarPlot($accBarPlot);

$graph->Add($gbarplot);

/*echo "<pre>";
print_r($data);
print_r($dataAFact);
echo "</pre>";*/

$graph->stroke();
?>
